package com.ymatou.iapi.prodstock.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;



import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;



import com.ymatou.iapi.liveseller.parameter.ActivityProductDto;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.LiveActivityDto;
import com.ymatou.iapi.prodstock.parameter.GetActivityProductListBean;
import com.ymatou.iapi.prodstock.parameter.LiveCatalogProduct;
import com.ymatou.iapi.prodstock.parameter.ProductActivityStockDto;
import com.ymatou.iapi.prodstock.service.GetActivityProductListCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductActivityStockWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*获取直播活动商品列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetActivityProductList{
	private static GetActivityProductListBean getactivityproductlistBean;
	private static GetActivityProductListCall getactivityproductlistCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static ArrayList<Integer> alist;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取直播活动商品列表");
	}

	@Before
	public void caseUp() {
		getactivityproductlistBean=new GetActivityProductListBean();
		getactivityproductlistCall=new GetActivityProductListCall();
		alist = new ArrayList<Integer>();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getactivityproductlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetActivityProductList_001() {
		Logger.start(true,"获取直播活动商品列表,平台活动");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			String Activityprice = "10.00";
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,Activityprice);
			
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			getactivityproductlistBean.setUserId(sellerId);
			getactivityproductlistBean.setActivityType(0);
			alist.add(activityId);
			getactivityproductlistBean.setActivityIdList(alist);
			getactivityproductlistCall.setData(getactivityproductlistBean);
			getactivityproductlistCall.callService();			
			
			Integer num = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			String Code = getactivityproductlistCall.getString("Code");
			Logger.verifyEquals("200", Code, "验证Code");
			getactivityproductlistCall.getLiveActivityList().forEach(pp->{
				Logger.verifyEquals(activityId, pp.getActivityId(), "验证活动id");
				pp.getLiveCatalogProductList().forEach(dd->{
					//int i = 0;
					Logger.verifyEquals(productId, dd.getProductId(), "验证商品编号");
					//Logger.verifyEquals(catalogs.get(i), dd.getCatalogId(), "验证规格编号");
					Logger.verifyEquals(num, dd.getStockNum(), "验证商品活动库存");
					Logger.verifyEquals(Activityprice, dd.getActivityPrice().setScale(2).toString(), "验证商品活动价格");
					//i++;
				});;
			});
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetActivityProductList_002() {
		Logger.start(true,"获取直播活动商品列表,秒杀活动");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			String price = "10.00";
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,price);
			
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			getactivityproductlistBean.setUserId(sellerId);
			getactivityproductlistBean.setActivityType(1);
			alist.add(activityId);
			getactivityproductlistBean.setActivityIdList(alist);
			getactivityproductlistCall.setData(getactivityproductlistBean);
			getactivityproductlistCall.callService();			
			
			Integer num = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			String Code = getactivityproductlistCall.getString("Code");
			Logger.verifyEquals("200", Code, "验证Code");
			getactivityproductlistCall.getLiveActivityList().forEach(pp->{
				Logger.verifyEquals(activityId, pp.getActivityId(), "验证活动id");
				pp.getLiveCatalogProductList().forEach(dd->{
					//int i = 0;
					Logger.verifyEquals(productId, dd.getProductId(), "验证商品编号");
					//Logger.verifyEquals(catalogs.get(i), dd.getCatalogId(), "验证规格编号");
					Logger.verifyEquals(num, dd.getStockNum(), "验证商品活动库存");
					Logger.verifyEquals(price, dd.getActivityPrice().setScale(2).toString(), "");
					//i++;
				});;
			});
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetActivityProductList_003() {
		Logger.start(true,"获取直播活动商品列表,秒杀活动");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			double price = addpro.getProduct().getCatalogList().get(0).getPrice();
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 60 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 6d, 6, starttime);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			
			Integer activityStock = createliveactivityBean.getLiveActivity().getActivityProducts().get(0).getActivityStock();
			Double activityPrice = createliveactivityBean.getLiveActivity().getActivityProducts().get(0).getActivityPrice();
			
			LiveActivityDto LiveActivity=createliveactivityBean.getLiveActivity();
			LiveRoomWapper LiveRoomWapper = new LiveRoomWapper();
			//直播秒杀活动id
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			LiveActivity.setLiveActivityId(liveacctivityid);
			//直播秒杀商品在活动中的id
			Integer LiveActivityProductId = LiveRoomWapper.selectLiveactivityproduct(liveacctivityid, LiveActivity.getActivityProducts().get(0).getProductId()).get(0).getLiveActivityProductId();
			ArrayList<Integer> list = new ArrayList<Integer>();
			list.add(liveacctivityid);
			getactivityproductlistBean.setActivityIdList(list);
			getactivityproductlistBean.setActivityType(1);
			getactivityproductlistBean.setUserId(sellerId);
			getactivityproductlistCall.setData(getactivityproductlistBean);
			getactivityproductlistCall.callService();		
			
			String Code = getactivityproductlistCall.getString("Code");
			Logger.verifyEquals("200", Code, "验证Code");
			
			getactivityproductlistCall.getLiveActivityList().forEach(pp->{
				Logger.verifyEquals(liveacctivityid, pp.getActivityId(), "验证ActivityId");
				pp.getLiveCatalogProductList().forEach(dd->{
					Logger.verifyEquals(productId, dd.getProductId(), "验证ProductId");
					Logger.verifyEquals(LiveActivityProductId, dd.getProductInActivityId(), "验证ProductInActivityId");
					Logger.verifyEquals(cids.get(0), dd.getCatalogId(), "验证CatalogId");
					Logger.verifyEquals(activityStock, dd.getStockNum(), "验证StockNum");
					Logger.verifyEquals(BigDecimal.valueOf(price), dd.getPrice().setScale(1), "验证Price");
					Logger.verifyEquals(BigDecimal.valueOf(activityPrice), dd.getActivityPrice().setScale(1), "验证ActivityPrice");
				});
			});;
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetActivityProductList_004() {
		Logger.start(true,"获取直播活动多个商品列表,秒杀活动");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			double price = addpro.getProduct().getCatalogList().get(0).getPrice();
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 60 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 6d, 6, starttime);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			
			Integer activityStock = createliveactivityBean.getLiveActivity().getActivityProducts().get(0).getActivityStock();
			Double activityPrice = createliveactivityBean.getLiveActivity().getActivityProducts().get(0).getActivityPrice();
			
			LiveActivityDto LiveActivity=createliveactivityBean.getLiveActivity();
			LiveRoomWapper LiveRoomWapper = new LiveRoomWapper();
			//直播秒杀活动id
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			LiveActivity.setLiveActivityId(liveacctivityid);
			//直播秒杀商品在活动中的id
			Integer LiveActivityProductId = LiveRoomWapper.selectLiveactivityproduct(liveacctivityid, LiveActivity.getActivityProducts().get(0).getProductId()).get(0).getLiveActivityProductId();
			ArrayList<Integer> list = new ArrayList<Integer>();
			list.add(liveacctivityid);
			getactivityproductlistBean.setActivityIdList(list);
			getactivityproductlistBean.setActivityType(1);
			getactivityproductlistBean.setUserId(sellerId);
			getactivityproductlistCall.setData(getactivityproductlistBean);
			getactivityproductlistCall.callService();		
			
			String Code = getactivityproductlistCall.getString("Code");
			Logger.verifyEquals("200", Code, "验证Code");
			
			getactivityproductlistCall.getLiveActivityList().forEach(pp->{
				Logger.verifyEquals(liveacctivityid, pp.getActivityId(), "验证ActivityId");
				pp.getLiveCatalogProductList().forEach(dd->{
					Logger.verifyEquals(productId, dd.getProductId(), "验证ProductId");
					Logger.verifyEquals(LiveActivityProductId, dd.getProductInActivityId(), "验证ProductInActivityId");
					Logger.verifyEquals(cids.get(0), dd.getCatalogId(), "验证CatalogId");
					Logger.verifyEquals(activityStock, dd.getStockNum(), "验证StockNum");
					Logger.verifyEquals(BigDecimal.valueOf(price), dd.getPrice().setScale(1), "验证Price");
					Logger.verifyEquals(BigDecimal.valueOf(activityPrice), dd.getActivityPrice().setScale(1), "验证ActivityPrice");
				});
			});;
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}